package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.data.analytics.enums.eUserIdAttr;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 70.数据录入上传数数（新通用接口）
 */
@RestController
@RequestMapping("/game/admin")
public class AdminDataThinkController {

    /**
     *  70.1 新增/修改
     */
    @ResponseBody
    @RequestMapping(value = "counting/uploadEvent", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String event(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        String eventName = jsonObject.getString("eventName");
        int type = jsonObject.getIntValue("type");

        JSONArray data = jsonObject.getJSONArray("data");
        for (int i = 0; i < data.size(); i++) {
            JSONObject map = data.getJSONObject(i);
            Map<String, Object> dataMap = new HashMap<>();
            handleTypeKey(eventName, type, dataMap);
            dataMap.put(eUserIdAttr.DISTINCT_ID.getAttrId(), "");
            dataMap.put("channelId", channelId);
            String accountId = "";
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if(entry.getValue() == null){
                    continue;
                }
                if("playerId".equals(entry.getKey())){
                    long playerId = map.getLongValue("playerId");
                    if(playerId > 0){
                        dataMap.put(eUserIdAttr.ACCOUNT_ID.getAttrId(), playerId);
                        accountId = map.getString("playerId");
                    }
                }
                handleKeyValue(eventName, entry.getKey(), entry.getValue(), dataMap);
            }
            String event_name = handleEventName(eventName, dataMap);

            DataAnalyticsMgr.provider.track(accountId, "", event_name, dataMap);
        }

        return new AdminResult().toJsonString();
    }

    private void handleTypeKey(String eventName, int type, Map<String, Object> dataMap) {
        if("ad_recharge".equals(eventName)){
            dataMap.put("pay_pattern", type);
        }else {
            dataMap.put("type", type);
        }
    }

    private String handleEventName(String eventName, Map<String, Object> dataMap) {
        return eventName;
    }

    private void handleKeyValue(String eventName, String key, Object value, Map<String, Object> dataMap) {
        //事件时间
        if("time".equals(key)){
            dataMap.put("#time", value);
            return;
        }
        if("playerId".equals(key)){
            dataMap.put("playerId", value);
            dataMap.put("userid_id", value);
            return;
        }
        //大掌柜，特殊处理广告事件
        if("ad_recharge".equals(eventName)){
            if("currency".equals(key)){
                dataMap.put("pay_type", value);
                return;
            }
            if("newMoney".equals(key)){
                dataMap.put("pay_amount", Integer.parseInt((String)value));
                return;
            }
        }
        dataMap.put(key, value);
    }
}
